package game.ground 
{
	import assets.Assets;
	import game.core.CollisionTypes;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Tilemap;
	import net.flashpunk.masks.Grid;
	/**
	 * ...
	 * @author rnk
	 */
	public class Ground extends Entity
	{
		
		private var tilemap:Tilemap;
		private var grid:Grid;
		public var startingPoses:Array = [];
		
		public function Ground(map:Class) 
		{
			
			tilemap = new Tilemap(Assets.TILES2_PNG, 800, 600, 32, 32);
			graphic = tilemap;
			layer = 1;
			type = CollisionTypes.ground;
			grid = new Grid(800, 600, 32, 32, 0, 0);
			mask = grid;
			
			LoadMap(map);
		}
		
		private function LoadMap(xml:Class):void 
		{
			var levelData:XML = FP.getXML(xml);
			var dataList:XMLList;
			var dataElement:XML;
			
			//tiles
			dataList = levelData.tiles.tile;
			for each (dataElement in dataList)
			{
				tilemap.setTile(int(dataElement.@x) / 32, int(dataElement.@y) / 32, GetTileId(dataElement.@tx,dataElement.@ty));
				grid.setTile(int(dataElement.@x) / 32, int(dataElement.@y) / 32, true);
			}
			
			//sectors
			dataList = levelData.tiles.rect;
			for each (dataElement in dataList)
			{
				tilemap.setRect(int(dataElement.@x) / 32, int(dataElement.@y) / 32, int(dataElement.@w) / 32, int(dataElement.@h) / 32, GetTileId(dataElement.@tx,dataElement.@ty));
				grid.setRect(int(dataElement.@x) / 32, int(dataElement.@y) / 32, int(dataElement.@w) / 32, int(dataElement.@h) / 32, true);
			}
			
			//starting poses
			startingPoses = [];
			dataList = levelData.objects.playerStart;
			for each (dataElement in dataList)
			{
				startingPoses.push( { x:int(dataElement.@x) / 32, y:int(dataElement.@y) / 32 } );
			}
			
		}
		
		
		private function GetTileId(tx:Number, ty:Number):int
		{
			var columns:int = 4;
			return int(tx / 32) + columns*int(ty / 32);
		}
		
		
		
	}

}